home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3256 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.mindlink.net!news
  2. From: genew@mindlink.bc.ca (Gene Wirchenko)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: division problem
  5. Date: Sat, 27 Jan 1996 08:32:31 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4eco1i$aih@fountain.mindlink.net>
  8. References: <31097D77.11AA@rain.org> <26JAN199622082450@erich.triumf.ca>
  9. NNTP-Posting-Host: line105.nwm.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. bennett@erich.triumf.ca (P.Bennett) wrote:
  13.  
  14. >In article <31097D77.11AA@rain.org>, tpaul <tpaul@rain.org> writes...
  15. >>Can anyone show me why this does not work?  I am a beginner.
  16. >> 
  17. >>#include <stdio.h>
  18. >> 
  19. >>main ()
  20. >>{
  21. >>    int fahrenheit, celsius;
  22. >>    
  23. >>    printf("Fahrenheit temperature =?";
  24. >>    scanf("%d",fahrenheit);
  25.                    ^
  26.      scanf() operates on pointers/addresses.  Your statement should
  27. be:
  28.           scanf("%d", &fahrenheit);
  29.                       ^
  30.  
  31. >>    celsius = 5/9*(fahrenheit-32);
  32.  
  33. >This calculation will be done with ints, and 5/9 in integer arithmetic is 0.
  34.  
  35. >Also (if I have the precedence rules right), it will be done as:
  36. >        5
  37. >    ----------------------
  38. >    9 * (fahrenheit - 32)
  39.  
  40.      Nope!  Multiplication and division have the same precedence level
  41. and are left to right evaluation.
  42.  
  43. >In order to make it work, you could do:
  44. >    celsius = (int)((5.0/9.0) * (fahrenheit - 32))
  45. >The 5.0/9.0 give a meaningful result, and force the multiplication to be done
  46. >with floats. The (int) will convert the result back to an int.
  47.  
  48.      A further note: be sure that you cast the result of the entire
  49. calculation to int, not just the 5/9 part or you get the same problem.
  50.  
  51.      The printf() from the original post is mising and wrong.  It's
  52. missing the closing right paren.
  53.  
  54. >Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  55. >Internet: bennett@triumf.ca         | of one another only when one can be
  56. >Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  57. >TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  58. >GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  59.  
  60. Sincerely,
  61.  
  62. Gene Wirchenko
  63.  
  64. C Pronunciation Guide:
  65.      y=x++;     "wye equals ex plus plus semicolon"
  66.      x=x++;     "ex equals ex doublecross semicolon"
  67.  
  68.